﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Sec;
using ESOL.Controls;
using System.Reflection;

namespace ESOL.WinUI
{
    public partial class frmDesktop : BaseForm.frmBaseMdiChildForm
    {
        private SecMenuEntity mSelectedMenu;
        private SecMenuEntity mLastSelectedMenu;

        public frmDesktop()
        {
            InitializeComponent();
        }

        private void InitShortcutIcon()
        {
            gboxMain.SuspendLayout();
            gboxMain.Controls.Clear();
            string strMenuID = "";
            List<SecMenuEntity> list = new List<SecMenuEntity>();
            if (mSelectedMenu == null)
            {
                strMenuID = "";
                lblSelectedMenu.Text = "所有模块";
                pboxBack.Enabled = false;
                pboxGo.Enabled = true;
            }
            else
            {
                strMenuID = mSelectedMenu.MenuId;
                lblSelectedMenu.Text = mSelectedMenu.MenuName;
                pboxBack.Enabled = true;
                pboxGo.Enabled = false;
            }
            if (CommonParameter.CurrentUser.PersonId == "1")
            {
                list = SecMenuBs.GetMenusByParentID(strMenuID);
            }
            else
            {
                list = SecMenuBs.GetMenusByParentID(strMenuID, Common.CommonParameter.CurrentUser.PersonId);
            }
            int x = 0;
            int y = 0;
            int spaceX = 24;
            int spaceY = 24;
            EsolShortcut sample = new EsolShortcut();
            int maxCount = (int)((gboxMain.Height - 48) / (sample.Height + spaceY));
            if (maxCount < 1)
            {
                maxCount = 1;
            }
            foreach (SecMenuEntity entity in list)
            {
                Assembly ass = Assembly.GetExecutingAssembly();
                EsolShortcut shortcut = new EsolShortcut();
                shortcut.BackColor = Color.FromArgb(227, 241, 254);
                if (string.IsNullOrEmpty(entity.ImageName) || ass.GetManifestResourceStream(entity.ImageName) == null)
                {
                    shortcut.ShortcutIcon = Properties.Resources.DefaultIcon;
                }
                else
                {
                    shortcut.ShortcutIcon = Image.FromStream(ass.GetManifestResourceStream(entity.ImageName));
                }
                shortcut.Text = entity.MenuName;
                shortcut.Tag = entity;
                y++;
                if (y > maxCount)
                {
                    x++;
                    y = 1;
                }
                shortcut.Location = new Point(12 + (shortcut.Width + spaceX) * x, 25 + (shortcut.Height + spaceY) * (y - 1));
                shortcut.DoubleClickShortcut = doDoublickClickShort;
                gboxMain.Controls.Add(shortcut);
            }
            gboxMain.ResumeLayout();
        }

        private void OpenMaster(SecMenuEntity menu)
        {
            if (menu == null)
            {
                return;
            }
            List<SecMenuEntity> menus = SecMenuBs.GetMenusByParentID(menu.MenuId);
            if (menus.Count == 0)
            {
                return;
            }
            foreach (SecMenuEntity entity in menus)
            {
                if (entity.MasterFlag == 1)
                {
                    foreach (Form frmOpen in this.MdiChildren)
                    {
                        if (frmOpen.Tag != null && frmOpen.Tag.Equals(entity.MenuId))
                        {
                            frmOpen.BringToFront();
                            return;
                        }
                    }
                    LoadForm(entity);
                    break;
                }
            }
        }

        protected override void OnShown(EventArgs e)
        {
            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
            try
            {
                InitShortcutIcon();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void doDoublickClickShort(object tag)
        {
            if (tag != null)
            {
                SecMenuEntity menu = (SecMenuEntity)tag;
                if (menu.MenuTypeCode == "10")
                {
                    mSelectedMenu = menu;
                    InitShortcutIcon();
                    OpenMaster(menu);
                }
                else if (menu.MenuTypeCode == "20" && string.IsNullOrEmpty(menu.FormName) == false)
                {
                    LoadForm(menu);
                }
            }
        }

        private void LoadForm(SecMenuEntity menu)
        {
            Assembly ass = Assembly.GetExecutingAssembly();
            Form frm = (Form)ass.CreateInstance(menu.FormName);
            foreach (Form frmOpen in this.MdiParent.MdiChildren)
            {
                if (frmOpen.Name == frm.Name)
                {
                    frmOpen.BringToFront();
                    return;
                }
            }
            frm.Text = menu.MenuName;
            frm.Tag = menu.MenuId;
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void pboxGo_Click(object sender, EventArgs e)
        {
            if (mLastSelectedMenu == null)
            {
                return;
            }
            mSelectedMenu = SecMenuBs.GetMenuByID(mLastSelectedMenu.MenuId);
            mLastSelectedMenu = null;
            InitShortcutIcon();
        }

        private void pboxBack_Click(object sender, EventArgs e)
        {
            if (mSelectedMenu != null)
            {
                mLastSelectedMenu = SecMenuBs.GetMenuByID(mSelectedMenu.MenuId);
            }
            mSelectedMenu = null;
            InitShortcutIcon();
        }

        private void pboxBack_MouseEnter(object sender, EventArgs e)
        {
            pboxBack.BackColor = Color.LightBlue;
        }

        private void pboxBack_MouseLeave(object sender, EventArgs e)
        {
            pboxBack.BackColor = Color.Transparent;
        }

        private void pboxGo_MouseEnter(object sender, EventArgs e)
        {
            pboxGo.BackColor = Color.LightBlue;
        }

        private void pboxGo_MouseLeave(object sender, EventArgs e)
        {
            pboxGo.BackColor = Color.Transparent;
        }

    }
}
